package com.example.administrator.mybabymall;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.administrator.mybabymall.bean.Tab;
import com.example.administrator.mybabymall.fragment.FragmentFive;
import com.example.administrator.mybabymall.fragment.FragmentFour;
import com.example.administrator.mybabymall.fragment.FragmentOne;
import com.example.administrator.mybabymall.fragment.FragmentThree;
import com.example.administrator.mybabymall.fragment.FragmentTwo;

import java.util.ArrayList;
import java.util.List;

/*
* 设置5个fragment
* 用于显示五个基础界面
*
* */
public class MainActivity extends AppCompatActivity {
//创建
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    List<Tab> tabs = new ArrayList<>(5);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTab();
    }

    private void initTab() {
//      初始化界面的text，drawer，fragment
        Tab home = new Tab(R.string.home,R.drawable.selector_icon_home,FragmentOne.class);
        Tab hot = new Tab(R.string.hot,R.drawable.selector_icon_hot,FragmentTwo.class);
        Tab car = new Tab(R.string.car,R.drawable.selector_icon_cart,FragmentThree.class);
        Tab catagory = new Tab(R.string.catagory,R.drawable.selector_icon_category,FragmentFour.class);
        Tab mine = new Tab(R.string.mine,R.drawable.selector_icon_mine,FragmentFive.class);
//      把Tab对象添加进人tabs集合
        tabs.add(home);
        tabs.add(hot);
        tabs.add(catagory);
        tabs.add(car);
        tabs.add(mine);
//      实例LayoutInflater
        mInflater = LayoutInflater.from(this);
//        绑定tabhost的view
        mTabHost = this.findViewById(android.R.id.tabhost);
        mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

        for (Tab tab : tabs){
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));
            View view = initIndicator(tab);
            tabSpec.setIndicator(view);
            mTabHost.addTab(tabSpec, tab.getFragment(),null);
        }

//        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
//        mTabHost.setCurrentTab(0);

    }
//初始化切换栏元素
    private  View initIndicator(Tab tab){
        View view = mInflater.inflate(R.layout.tab_indicator,null);
//        绑定切换栏的image和text
        ImageView imageView = view.findViewById(R.id.icon_tab);
        TextView textView = view.findViewById(R.id.txt_indicator_tab);
//        为每个tab的text和image赋值
        imageView.setBackgroundResource(tab.getIcon());
        textView.setText(tab.getTitle());
        return view;
    }
}
